﻿namespace Home
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    public static class HandleErrorHelper
    {
        public static void Handle(Exception exception)
        {
            Debug.Assert(exception != null);

            var message = Format(exception);
            Trace.TraceError(message);
        }

        private static string Format(Exception exception)
        {
            var result = new StringBuilder();

            do
            {
                result.AppendFormat("{1}{0}", Environment.NewLine, exception.Message);
                result.AppendFormat("{1}{0}", Environment.NewLine, exception.StackTrace);

                exception = exception.InnerException;
            } while (exception != null);

            return result.ToString();
        }
    }
}
